﻿using System;
using System.Collections.Generic;
using System.Text;
using Entites;
using Core;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Services
{
    public class MenuPermissionApp 
    {

        public ApplicationDbContext _db;
        public MenuPermissionApp(ApplicationDbContext db) 
        {
            this._db = db;


        }

        public List<Menu> GetMenus(User u )
        {

            List<Menu> menus = new List<Menu>();
           var Roles = _db.User.Where(c => c.username == u.username).FirstOrDefault().UserRoles.ToList();
            
            foreach(var item in Roles )
            {
                
                //预先加载
                var mn = _db.MenuPermission.Include(c=>c.menu). Where(c => c.role.Id == item.Id).ToList();
             
                foreach (var ms in mn)
                {
                  
                    if (!menus.Contains(ms.menu))
                        menus.Add(ms.menu);
                
                }

            }
            return menus;
            }



    }
}
